package com.example.bookshelf.data;

import android.util.Log;

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpDataLoader {
    static String API_URL = "https://api.jike.xyz/situ/book/isbn/";
    static String API_KEY = "?apikey=14514.7966a1e7ba9c727138580c3ae3c5372b.0f1f31512707df7479961f742eab5dff";

    public static String getHttpPath(String ISBN) {
        return API_URL + ISBN + API_KEY;
    }


    @NonNull
    public static String getHttpData(String path){
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);

            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream());
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String tempLine = null;
                StringBuffer resultBuffer = new StringBuffer();
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                    resultBuffer.append("\n");
                }
                return resultBuffer.toString();
            }
        }
        catch (Exception e) {
           e.printStackTrace();
            Log.i("error", "getHttpData: " + e.toString());
        }
        return "";
    }

    static public BookItem ParseJsonData(String JSONText, String ISBN) {
        if (JSONText.equals(""))
            return null;

        try {
            BookItem bookItem = new BookItem();
            JSONObject root = new JSONObject(JSONText);
            JSONObject data = root.getJSONObject("data");


            if (!data.getString("name").equals("null"))
                bookItem.setTitle(data.getString("name"));

            if (!data.getString("author").equals("null"))
                bookItem.setAuthor(data.getString("author"));

            if (!data.getString("translator").equals("null"))
                bookItem.setTranslator(data.getString("translator"));

            if (!data.getString("publishing").equals("null"))
                bookItem.setPress(data.getString("publishing"));

            if(!data.getString("published").equals("null")) {
                String date = data.getString("published");
                String[] year_month = date.split("-");
                bookItem.setPublishedYear(Integer.parseInt(year_month[0]));
                bookItem.setPublishedMonth(Integer.parseInt(year_month[1]));
            }

            bookItem.setISBN(ISBN);
            return bookItem;

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    static public BookItem getBookItemByISBN(String ISBN) {
        String path = getHttpPath(ISBN);
        String jsonText = getHttpData(path);

        return ParseJsonData(jsonText, ISBN);
    }


}
